package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import oracle.sql.DATE;
import pojo.Book;
import pojo.Order;
import pojo.OrderShow;
import pojo.User;
import pojo.dao.manager.PojoDaoManager;
import pojo.dao.manager.impl.PojoDaoManagerFactory;

/**
 * Servlet implementation class BuyBookServlet
 */
public class BuyBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Map<String, Integer> mapId=new HashMap<String, Integer>();
	private static Map<String, String> mapName=new HashMap<String, String>();
	private static PojoDaoManager manager=PojoDaoManagerFactory.getInstance();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuyBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("get a buy infomation");
		Connection connection=null;
		try {
			Context initContext = new InitialContext();
			DataSource ds  = (DataSource) initContext.lookup("java:/comp/env/jdbc/myoracle");
			connection = ds.getConnection();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String buyType=request.getParameter("type");
		if(buyType.equals("buy")){
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			if(mapId.containsKey(id))
			{
				int m=mapId.get(id)+1;
				mapId.remove(id);
				mapId.put(id, m);
			}
			else {
				mapId.put(id, 1);
				mapName.put(id, name);
			}
			request.getRequestDispatcher("shop").forward(request, response);
		}
		else if(buyType.equals("showcart")){
			List<OrderShow> list=new ArrayList<>();
			Set<String> idSet=mapId.keySet();
			Iterator<String> iterable=idSet.iterator();
			while(iterable.hasNext())
			{
				String idBook=iterable.next();
				OrderShow tempOrder=new OrderShow();
				tempOrder.setId(idBook);
				tempOrder.setCount(mapId.get(idBook));
				tempOrder.setBookname(mapName.get(idBook));
				list.add(tempOrder);
			}
			request.setAttribute("cartlist",list);
			request.getRequestDispatcher("../booklist.jsp").forward(request, response);
		}
		else{
			List<Order> list=new ArrayList<>();
			Set<String> idSet=mapId.keySet();
			Iterator<String> iterable=idSet.iterator();
			String userid= request.getParameter("userid");
			while(iterable.hasNext())
			{
				String idBook=iterable.next();
				Order tempOrder=new Order();
				tempOrder.setBookId(idBook);
				tempOrder.setCount(mapId.get(idBook));
				tempOrder.setDate(new Date());
				tempOrder.setUserId(userid);
				manager.addOrder(tempOrder, connection);
			}
			mapId.clear();
			mapName.clear();
			request.getRequestDispatcher("shop").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
